
package ca.kooki.codesnippet.views;

import junit.framework.Assert;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.junit.Before;
import org.junit.Test;

import ca.kooki.codesnippet.Activator;
import ca.kooki.codesnippet.CodeSnippet;
import ca.kooki.codesnippet.CodeSnippetRepository;
import ca.kooki.codesnippet.Tag;

public class TagEntryViewHandlerTest extends SwtTest {

  private KeyEvent keyEvent;
  
  private Button buttonAdd;
  private Text inputTitle;
  private Text inputKeywords;
  private Text inputCode;
  private Label labelMessage;
  private TagEntryViewHandler handler;
    
  @Before
  public void setUp() throws Exception {
    
    super.setUp();
    
    parent = new Shell(display);
    buttonAdd = new Button(parent,SWT.PUSH);
    inputTitle = new Text(parent,SWT.SINGLE);
    inputKeywords = new Text(parent,SWT.SINGLE);
    inputCode = new Text(parent,SWT.SINGLE);
    labelMessage = new Label(parent,SWT.LEFT);
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    
    Event event = new Event();
    event.widget = inputTitle;
    keyEvent = new KeyEvent(event);
    
    event = new Event();
    event.widget = inputTitle;
    
    Activator.getDefault().getPreferences().setValue("user", "aforward");
  }
  
  @Test
  public void EmptyTextDisableAdd()
  {
    inputTitle.setText("");
    inputKeywords.setText("");
    inputCode.setText("");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(false,buttonAdd.isEnabled());
  }
  
  @Test
  public void NoTextInputDisableAdd()
  {
    inputTitle.setText(" ");
    inputKeywords.setText("  ");
    inputCode.setText(" ");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(false,buttonAdd.isEnabled());
  }
  
  @Test
  public void HasTextEnableText()
  {
    inputTitle.setText("a");
    inputKeywords.setText("b");
    inputCode.setText("c");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(true,buttonAdd.isEnabled());  
  }

  @Test
  public void NotAllTextSetCannotAdd()
  {
    inputTitle.setText("a");
    inputKeywords.setText("");
    inputCode.setText("");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(false,buttonAdd.isEnabled());  
  }

  
  @Test
  public void ConstructorUpdatesAdd()
  {
    inputTitle.setText("");
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    Assert.assertEquals(false,buttonAdd.isEnabled());
    
    inputTitle.setText("b");
    inputCode.setText("a");
    inputKeywords.setText("c");
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    Assert.assertEquals(true,buttonAdd.isEnabled());
  }

  @Test
  public void MustProvideCode()
  {
    inputTitle.setText(" ");
    inputKeywords.setText(" ");
    inputCode.setText(" ");
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.addNewTag();
    
    Assert.assertEquals("Please provide code snippet", labelMessage.getText());
  }

  @Test
  public void NotLoggedIn()
  {
    Activator.resetDefault();
    
    inputTitle.setText(" ");
    inputKeywords.setText(" ");
    inputCode.setText("x");
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.addNewTag();
    
    Assert.assertEquals("Please set your username in the preferences", labelMessage.getText());
  }

  @Test
  public void AfterSavingRemoveTheSuccessNote()
  {
    inputTitle.setText(" ");
    inputKeywords.setText(" ");
    inputCode.setText("x");
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.addNewTag();
    
    handler.keyReleased(keyEvent);
    Assert.assertEquals("", labelMessage.getText());
  }

  @Test
  public void JustTheCodeNeedsToBeSet()
  {
    
    inputTitle.setText(" ");
    inputKeywords.setText(" ");
    inputCode.setText("x");
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.addNewTag();
    
    Assert.assertEquals("Success!", labelMessage.getText());
  }
  
  @Test
  public void AddTag()
  {
    Activator.getDefault().getPreferences().setDefault("user", "aforward");
    inputTitle.setText("abc");
    inputKeywords.setText("def");
    inputCode.setText("hij");
    
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.addNewTag();
    
    Assert.assertEquals("Success!", labelMessage.getText());
    
    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    
    Tag[] allTags = repository.getAllTags("/aforward");
    Assert.assertEquals(1, allTags.length);
    
    Tag expected = new Tag("abc","def",new CodeSnippet("abc","def","hij"),"aforward");
    
    Assert.assertEquals(expected, allTags[0]);
    
  }
  
  @Test
  public void DoNotTrimAddedItem()
  {
    inputTitle.setText("  abc  ");
    inputKeywords.setText("def   ");
    inputCode.setText("  hij");
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.addNewTag();
    
    Assert.assertEquals("Success!", labelMessage.getText());
    
    CodeSnippetRepository repository = Activator.getDefault().getRepository();

    Assert.assertEquals("  abc  ", repository.getAllSnippets()[0].getTitle());
    Assert.assertEquals("def   ", repository.getAllSnippets()[0].getKeywords());
    Assert.assertEquals("  hij", repository.getAllSnippets()[0].getCode());
  }
  
  
  @Test
  public void DoNotClearNewToDoInputAfterAdd()
  {
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    inputTitle.setText("aha");
    inputKeywords.setText("def");
    inputCode.setText("hij");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(true, buttonAdd.isEnabled());
    
    handler.addNewTag();
    
    Assert.assertEquals("aha",inputTitle.getText());
    Assert.assertEquals(true, buttonAdd.isEnabled());
  }
  
  @Test
  public void PressNumEnterdoesNotAddNewItem()
  {
    keyEvent.keyCode = SWT.KEYPAD_CR;
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    inputTitle.setText("aha");
    inputKeywords.setText("def");
    inputCode.setText("hij");
    handler.keyReleased(keyEvent);

    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    Assert.assertEquals(0, repository.getAllSnippets().length);
  }

  @Test
  public void PressEnterDoesNotAddNewItem()
  {
    keyEvent.keyCode = SWT.CR;
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    inputTitle.setText("aha");
    inputKeywords.setText("def");
    inputCode.setText("hij");
    handler.keyReleased(keyEvent);

    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    Assert.assertEquals(0, repository.getAllSnippets().length);
  }

  @Test
  public void UpdateTag()
  {
    
    CodeSnippet mysnippet = new CodeSnippet("myt","myk1 myk2 myk3","void(0)");
    Tag tag = new Tag("t","k1 k2 k3",mysnippet,"aforward");
    
    Activator.getDefault().getRepository().setUser("james");
    handler.updateTag(tag);
    
    Assert.assertEquals(tag.getTitle(), inputTitle.getText());
    Assert.assertEquals(tag.getKeywords(), inputKeywords.getText());
    Assert.assertEquals(tag.getCodeSnippet().getCode(), inputCode.getText());
    Assert.assertEquals(tag, handler.tagToUpdate);
    Assert.assertEquals(true,buttonAdd.isEnabled());
  }

  
  @Test
  public void TagExistingCodeSnippetFromSearchPage()
  {
    
    CodeSnippet mysnippet = new CodeSnippet("myt","myk1 myk2 myk3","void(0)");
    Tag tag = new Tag("t","k1 k2 k3",mysnippet,"aforward");
    
    handler.tagExistingCodeSnippet(tag);
    
    Assert.assertEquals(tag.getTitle(), inputTitle.getText());
    Assert.assertEquals(tag.getKeywords(), inputKeywords.getText());
    Assert.assertEquals(tag.getCodeSnippet().getCode(), inputCode.getText());
    Assert.assertEquals(tag.getCodeSnippet(), handler.codeSnippet);
    Assert.assertEquals(true,buttonAdd.isEnabled());
  }
  
  
  @Test
  public void UpdateTagFromExistingTag()
  {
    Activator.getDefault().getPreferences().setDefault("user", "aforward");
    inputTitle.setText("abc");
    inputKeywords.setText("def");
    inputCode.setText("void(0)");
    
    CodeSnippet mysnippet = new CodeSnippet("myt","myk1 myk2 myk3","void(0)");
    Tag mytag = new Tag("t","k",mysnippet,"aforward");
    Activator.getDefault().getRepository().addTag(mytag);
    
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.tagToUpdate = mytag;
    handler.addNewTag();
    
    Assert.assertEquals("Success!", labelMessage.getText());
    
    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    
    Tag[] allTags = repository.getAllTags("/aforward");
    Assert.assertEquals(1, allTags.length);
    
    Assert.assertEquals(mytag.getId(), allTags[0].getId());
    Assert.assertEquals(null, handler.tagToUpdate);
  }
  
  @Test
  public void SaveTagFromExistingCodeSnippet()
  {
    Activator.getDefault().getPreferences().setDefault("user", "aforward");
    inputTitle.setText("abc");
    inputKeywords.setText("def");
    inputCode.setText("void(0)");
    
    CodeSnippet mysnippet = new CodeSnippet("myt","myk1 myk2 myk3","void(0)");
    Activator.getDefault().getRepository().addCodeSnippet(mysnippet);
    
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.codeSnippet = mysnippet;
    handler.addNewTag();
    
    Assert.assertEquals("Success!", labelMessage.getText());
    
    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    
    Tag[] allTags = repository.getAllTags("/aforward");
    Assert.assertEquals(1, allTags.length);
    
    Tag expected = new Tag("abc","def",mysnippet,"aforward");
    Assert.assertEquals(expected, allTags[0]);
    Assert.assertEquals(null, handler.codeSnippet);
    
  }
  
  @Test
  public void ModifyExistingCodeSnippetSavesNewTag()
  {
    Activator.getDefault().getPreferences().setDefault("user", "aforward");
    
    CodeSnippet mysnippet = new CodeSnippet("myt","myk1 myk2 myk3","void(0)");
    Activator.getDefault().getRepository().addCodeSnippet(mysnippet);
    
    handler = new TagEntryViewHandler(buttonAdd,inputTitle,inputKeywords,inputCode,labelMessage);
    handler.codeSnippet = mysnippet;
    inputTitle.setText("new title");
    inputKeywords.setText("new keywords");
    inputCode.setText("void(1)");
    handler.addNewTag();
    

    Assert.assertEquals("Success!", labelMessage.getText());
    
    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    
    Tag[] allTags = repository.getAllTags("/aforward");
    Assert.assertEquals(1, allTags.length);
    
    Assert.assertEquals("void(1)", allTags[0].getCodeSnippet().getCode());
    Assert.assertEquals(null, handler.codeSnippet);
    
  }
  

}
